package com.dykj.fireFighting.server.socialUnit.videoGroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.base.video.entity.Video;
import com.dykj.fireFighting.api.base.video.vo.VideoVo;
import com.dykj.fireFighting.api.socialUnit.videoGroup.VideoGroupDto;
import com.dykj.fireFighting.api.socialUnit.videoGroup.entity.VideoGroup;
import com.dykj.fireFighting.api.socialUnit.videoGroup.entity.VideoToGroup;
import com.dykj.fireFighting.api.socialUnit.videoGroup.service.IVideoGroupService;
import com.dykj.fireFighting.api.socialUnit.videoGroup.vo.VideoGroupVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.socialUnit.videoGroup.mapper.VideoGroupMapper;
import com.dykj.fireFighting.server.socialUnit.videoGroup.mapper.VideoToGroupMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 摄像头分组管理
 *
 * @Author JacksonTu
 * @Date  2022-02-28 14:25:03
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class VideoGroupServiceImpl extends BaseServiceImpl<VideoGroupMapper, VideoGroup> implements IVideoGroupService {
        @Autowired
        private VideoToGroupMapper videoToGroupMapper;
    @Override
    public PageVo<VideoGroupVo> queryList(VideoGroupDto videoGroupDto) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unitId",videoGroupDto.getUnitId());
        if(null !=videoGroupDto.getName() && StringUtils.isNotBlank(videoGroupDto.getName())){
            map.put("name","%" + videoGroupDto.getName() + "%");
        }
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(videoGroupDto.getPage());
        // 设置页大小
        page.setSize(videoGroupDto.getLimit());
        return new PageVo<VideoGroupVo>(this.baseMapper.queryList(page, map));

    }

    @Override
    public void insert(VideoGroup videoGroup) throws Exception {

        int insert = this.baseMapper.insert(videoGroup);
        for (String  vid: videoGroup.getVideoIds()
             ) {
            VideoToGroup videoToGroup = new VideoToGroup();
            videoToGroup.setId(UUIDUtils.get32UUID());
            videoToGroup.setGroupId(videoGroup.getId());
            videoToGroup.setVideoId(vid);
            videoToGroupMapper.insert(videoToGroup);
        }


    }

    @Override
    public VideoGroupVo getInfoById(String id) {
        return this.baseMapper.getIntoById(id);
    }

    @Override
    public void update(VideoGroup videoGroup) throws Exception {
        QueryWrapper<VideoToGroup> videoToGroupQueryWrapper = new QueryWrapper<>();
        videoToGroupQueryWrapper.eq("group_id",videoGroup.getId());
        int delete = videoToGroupMapper.delete(videoToGroupQueryWrapper);
        int i = this.baseMapper.updateById(videoGroup);
        for (String  vid: videoGroup.getVideoIds()
        ) {
            VideoToGroup videoToGroup = new VideoToGroup();
            videoToGroup.setId(UUIDUtils.get32UUID());
            videoToGroup.setGroupId(videoGroup.getId());
            videoToGroup.setVideoId(vid);
            videoToGroupMapper.insert(videoToGroup);
        }

    }

    @Override
    public void delete(List<String> asList) throws Exception {

        this.baseMapper.updateIsDel(asList);
        videoToGroupMapper.deleteByGroupId(asList);

    }

    @Override
    public List<VideoGroup> groupList(VideoGroupDto videoGroupDto) {
        QueryWrapper<VideoGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("unit_id",videoGroupDto.getUnitId());
        if(null !=videoGroupDto.getName() && StringUtils.isNotBlank(videoGroupDto.getName())){
            wrapper.like("name",videoGroupDto.getName());
        }
        List<VideoGroup> videoGroups = this.baseMapper.selectList(wrapper);
        return videoGroups;
    }

    @Override
    public List<VideoVo> getVideoByGroupUnit(VideoGroupDto videoGroupDto) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("unitId",videoGroupDto.getUnitId());
        if(videoGroupDto.getGroupId() !=null && StringUtils.isNotBlank(videoGroupDto.getGroupId())){
            params.put("groupId",videoGroupDto.getGroupId());
        }
       List<VideoVo> list = this.baseMapper.getVideoByGroupUnit(params);

        return list;
    }


}
